package com.chery.media.utils

import android.content.Context
import android.provider.MediaStore
import com.blankj.utilcode.util.LogUtils
import com.chery.media.library.extend.toJson
import com.chery.media.library.extend.toast
import com.chery.media.network.model.MusicBean

/**
 * 本地音乐帮助类
 */
object LocalMusicHelper {

    fun getLocalMusics(context: Context): ArrayList<MusicBean> {
        val list = arrayListOf<MusicBean>()
        val cursor = context.contentResolver.query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            null,
            null,
            null,
            MediaStore.Audio.Media.DEFAULT_SORT_ORDER
        )
        if (cursor != null) {
            while (cursor.moveToNext()) {
                val id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID))
                val title =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE))
                val artist =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST))
                val album =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM))
                val duration =
                    cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION))
                val size =
                    cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE))
                val url =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA))
                val albumId =
                    cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID))
                val albumArt =
                    cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_KEY))

                val music = MusicBean(title.split("-").first(), albumArt, duration.toString(), "$id", "", title.split("-").last(), url)
//                musicList.add(music)
                list.add(music)
            }
            cursor.close()
        }
        LogUtils.eTag("U盘音乐列表", list.toJson())
        return list
    }

}